{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Advanced Usage Example - extracting a single aspect with inner concepts from a legal document"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Advanced Usage Example - extracting a single aspect with inner concepts from a legal document\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM, StringConcept, StringExample\n",
        "\n",
        "# Create a document instance with e.g. a legal contract text\n",
        "# The text is shortened for brevity\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"EMPLOYMENT AGREEMENT\\n\\n\"\n",
        "        'This Employment Agreement (the \"Agreement\") is made and entered into as of January 15, 2023 (the \"Effective Date\"), '\n",
        "        'by and between ABC Corporation, a Delaware corporation (the \"Company\"), and Jane Smith, an individual (the \"Employee\").\\n\\n'\n",
        "        \"1. EMPLOYMENT TERM\\n\"\n",
        "        \"The Company hereby employs the Employee, and the Employee hereby accepts employment with the Company, upon the terms and \"\n",
        "        \"conditions set forth in this Agreement. The term of this Agreement shall commence on the Effective Date and shall continue \"\n",
        "        'for a period of two (2) years, unless earlier terminated in accordance with Section 8 (the \"Term\").\\n\\n'\n",
        "        \"2. POSITION AND DUTIES\\n\"\n",
        "        \"During the Term, the Employee shall serve as Chief Technology Officer of the Company, with such duties and responsibilities \"\n",
        "        \"as are commensurate with such position.\\n\\n\"\n",
        "        \"8. TERMINATION\\n\"\n",
        "        \"8.1 Termination by the Company. The Company may terminate the Employee's employment for Cause at any time upon written notice. \"\n",
        "        \"\\\"Cause\\\" shall mean: (i) Employee's material breach of this Agreement; (ii) Employee's conviction of a felony; or \"\n",
        "        \"(iii) Employee's willful misconduct that causes material harm to the Company.\\n\"\n",
        "        \"8.2 Termination by the Employee. The Employee may terminate employment for Good Reason upon 30 days' written notice to the Company. \"\n",
        "        \"\\\"Good Reason\\\" shall mean a material reduction in Employee's base salary or a material diminution in Employee's duties.\\n\"\n",
        "        \"8.3 Severance. If the Employee's employment is terminated by the Company without Cause or by the Employee for Good Reason, \"\n",
        "        \"the Employee shall be entitled to receive severance pay equal to six (6) months of the Employee's base salary.\\n\\n\"\n",
        "        \"IN WITNESS WHEREOF, the parties have executed this Agreement as of the date first written above.\\n\\n\"\n",
        "        \"ABC CORPORATION\\n\\n\"\n",
        "        \"By: ______________________\\n\"\n",
        "        \"Name: John Johnson\\n\"\n",
        "        \"Title: CEO\\n\\n\"\n",
        "        \"EMPLOYEE\\n\\n\"\n",
        "        \"______________________\\n\"\n",
        "        \"Jane Smith\"\n",
        "    )\n",
        ")\n",
        "\n",
        "# Define an aspect focused on termination clauses\n",
        "termination_aspect = Aspect(\n",
        "    name=\"Termination Provisions\",\n",
        "    description=\"Analysis of contract termination conditions, notice requirements, and severance terms.\",\n",
        "    reference_depth=\"paragraphs\",\n",
        ")\n",
        "\n",
        "# Define concepts for the termination aspect\n",
        "termination_for_cause = StringConcept(\n",
        "    name=\"Termination for Cause\",\n",
        "    description=\"Conditions under which the company can terminate the employee for cause.\",\n",
        "    examples=[  # optional, examples help the LLM to understand the concept better\n",
        "        StringExample(content=\"Employee may be terminated for misconduct\"),\n",
        "        StringExample(content=\"Termination for breach of contract\"),\n",
        "    ],\n",
        "    add_references=True,\n",
        "    reference_depth=\"sentences\",\n",
        ")\n",
        "notice_period = StringConcept(\n",
        "    name=\"Notice Period\",\n",
        "    description=\"Required notification period before employment termination.\",\n",
        "    add_references=True,\n",
        "    reference_depth=\"sentences\",\n",
        ")\n",
        "severance_terms = StringConcept(\n",
        "    name=\"Severance Package\",\n",
        "    description=\"Compensation and benefits provided upon termination.\",\n",
        "    add_references=True,\n",
        "    reference_depth=\"sentences\",\n",
        ")\n",
        "\n",
        "# Add concepts to the aspect\n",
        "termination_aspect.add_concepts([termination_for_cause, notice_period, severance_terms])\n",
        "\n",
        "# Add the aspect to the document\n",
        "doc.add_aspects([termination_aspect])\n",
        "\n",
        "# Create an LLM for extracting data from the document\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o\",  # You can use models from other providers as well, e.g. \"anthropic/claude-3-5-sonnet\"\n",
        "    api_key=os.environ.get(\n",
        "        \"CONTEXTGEM_OPENAI_API_KEY\"\n",
        "    ),  # your API key for OpenAI or another LLM provider\n",
        ")\n",
        "\n",
        "# Extract all information from the document\n",
        "doc = llm.extract_all(doc)\n",
        "\n",
        "# Access the extracted information in the document object\n",
        "print(\"=== Termination Provisions Analysis ===\")\n",
        "print(f\"Extracted {len(doc.aspects[0].extracted_items)} items from the aspect\")\n",
        "\n",
        "# Access extracted aspect concepts in the document object\n",
        "for concept in doc.aspects[0].concepts:\n",
        "    print(f\"--- {concept.name} ---\")\n",
        "    for item in concept.extracted_items:\n",
        "        print(f\"\u2022 {item.value}\")\n",
        "        print(f\"  Reference sentences: {len(item.reference_sentences)}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}